package com.tj.jinzao.fragment;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.tj.jinzao.R;
import com.tj.jinzao.activity.CompleteInfoActivity;
import com.tj.jinzao.base.BaseFragment;
import com.tj.jinzao.base.ConstantApp;
import com.tj.jinzao.callback.ICaseView;
import com.tj.jinzao.model.Target;
import com.tj.jinzao.presenter.CompleteInfoPresenter;
import com.tj.jinzao.utils.ToastUtils;

import java.util.List;
import java.util.Random;

import butterknife.BindColor;
import butterknife.BindView;
import butterknife.BindViews;
import butterknife.OnClick;


/**
 * 完善资料6-目标
 * Created by wjy on 2019/1/6.
 */
public class CompleteInfoStep6Fragment extends BaseFragment<CompleteInfoPresenter> implements ICaseView<List<Target>> {

    private final static String ARG_TYPE = "type";

    @BindViews({R.id.tv_text_1, R.id.tv_text_2, R.id.tv_text_3, R.id.tv_text_4, R.id.tv_text_5
            , R.id.tv_text_6, R.id.tv_text_7, R.id.tv_text_8, R.id.tv_text_9, R.id.tv_text_10
            , R.id.tv_text_11, R.id.tv_text_12, R.id.tv_text_13, R.id.tv_text_14, R.id.tv_text_15,
            R.id.tv_text_16, R.id.tv_text_17, R.id.tv_text_18, R.id.tv_text_19, R.id.tv_text_20
            , R.id.tv_text_21, R.id.tv_text_22, R.id.tv_text_23, R.id.tv_text_24, R.id.tv_text_25
            , R.id.tv_text_26, R.id.tv_text_27, R.id.tv_text_28, R.id.tv_text_29, R.id.tv_text_30})
    List<TextView> mTextViews;
    @BindView(R.id.tv_target_1)
    TextView mTarget1View;
    @BindView(R.id.tv_target_2)
    TextView mTarget2View;
    @BindView(R.id.bt_next)
    Button mNextView;

    @BindColor(R.color.complete_target_text_1)
    int mCompleteColor1;
    @BindColor(R.color.complete_target_text_2)
    int mCompleteColor2;
    private List<Target> mData;
    private int mType;

    public static CompleteInfoStep6Fragment newInstance(int type) {
        CompleteInfoStep6Fragment fragment = new CompleteInfoStep6Fragment();
        Bundle args = new Bundle(1);
        args.putInt(ARG_TYPE, type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void setupViews(LayoutInflater inflater, View view) {
        super.setupViews(inflater, view);
        for (TextView textView : mTextViews) {
            textView.setVisibility(View.INVISIBLE);
        }
        mType = getArguments().getInt(ARG_TYPE);
        if (mType == 1) {
            if(getActivity().getIntent()!=null&&getActivity().getIntent().getIntExtra("mType",1)==4){
                mTarget1View.setText("给用户打标签");
                mTarget2View.setText("让我们更好的知道您的情况");
                mTarget1View.setVisibility(View.GONE);
                mTarget2View.setVisibility(View.GONE);
                mPresenter.getPersonTargetZxs();
            }else {
                mTarget1View.setText("目前身体状况");
                mTarget2View.setText("让我们更好的知道您的情况");
                mPresenter.getPhysicalCondition();
                mNextView.setText("开启尽早之旅");
            }
        }else if(getActivity().getIntent()!=null&&getActivity().getIntent().getIntExtra("mType",1)==4){
            mTarget1View.setVisibility(View.GONE);
            mTarget2View.setVisibility(View.GONE);
            mPresenter.getPersonTargetZxs();
        } else {
            if(getActivity().getIntent()!=null&&(getActivity().getIntent().getIntExtra("mType",1)==2)||mType == 0){
                mPresenter.getPersonTarget();
            }else {
                mPresenter.getPhysicalCondition();
            }

        }

//        if(getActivity().getIntent().getIntExtra("mStep",1)==6){
//            mPresenter.getPersonTarget();
//        }else if(getActivity().getIntent().getIntExtra("mStep",1)==7){
//            mPresenter.getPhysicalCondition();
//        }
        if(getActivity().getIntent()!=null&&(getActivity().getIntent().getIntExtra("mType",1)==2||getActivity().getIntent().getIntExtra("mType",1)==3||getActivity().getIntent().getIntExtra("mType",1)==4)){
            mNextView.setText("确认");
        }

    }

    @Override
    protected CompleteInfoPresenter createPresenter() {
        return new CompleteInfoPresenter(this);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_complete_info_step_6;
    }

    @OnClick(R.id.bt_next)
    public void next() {
        if(ConstantApp.isFastDoubleClick(2000)){
            return;
        }
        if(mData==null||mData.size()==0){
            ToastUtils.showToast(getActivity(),"请选择");
            return;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (Target target : mData) {
            if (target.isSelect()) {
                if (stringBuilder.length() != 0) stringBuilder.append(",");
                if(getActivity().getIntent()!=null&&(getActivity().getIntent().getIntExtra("mType",1)==4)){
                    stringBuilder.append(target.getTag());
                }else {
                    stringBuilder.append(target.getTitle());
                }

            }
        }
        if(stringBuilder.length()==0){
            ToastUtils.showToast(getActivity(),"请选择");
            return;
        }
        if(getActivity().getIntent()!=null&&getActivity().getIntent().getIntExtra("mType",1)==2){
            Intent to=new Intent();
            to.putExtra("contentTarget",stringBuilder.toString());
            getActivity().setResult(Activity.RESULT_OK,to);
            getActivity().finish();
            return;
        }else if(getActivity().getIntent()!=null&&getActivity().getIntent().getIntExtra("mType",1)==3){
            Intent to=new Intent();
            to.putExtra("contentBody",stringBuilder.toString());
            getActivity().setResult(Activity.RESULT_OK,to);
            getActivity().finish();
            return;
        }else if(getActivity().getIntent()!=null&&getActivity().getIntent().getIntExtra("mType",1)==4){
            Intent to=new Intent();
            to.putExtra("contentName",stringBuilder.toString());
            getActivity().setResult(Activity.RESULT_OK,to);
            getActivity().finish();
            return;
        }

        if (mContext instanceof CompleteInfoActivity) {
            if (mType == 0) {
                if (stringBuilder.length() > 0) {
                    ((CompleteInfoActivity) mContext).setData("personalGoal", stringBuilder.toString());
                }
                ((CompleteInfoActivity) mContext).next();
            } else {
                if (stringBuilder.length() > 0) {
                    ((CompleteInfoActivity) mContext).setData("physicalConditions", stringBuilder.toString());
                }
                ((CompleteInfoActivity) mContext).next();
            }
        }
    }


    @OnClick({R.id.tv_text_1, R.id.tv_text_2, R.id.tv_text_3, R.id.tv_text_4, R.id.tv_text_5
            , R.id.tv_text_6, R.id.tv_text_7, R.id.tv_text_8, R.id.tv_text_9, R.id.tv_text_10
            , R.id.tv_text_11, R.id.tv_text_12, R.id.tv_text_13, R.id.tv_text_14, R.id.tv_text_15,
             R.id.tv_text_16, R.id.tv_text_17, R.id.tv_text_18, R.id.tv_text_19, R.id.tv_text_20
            , R.id.tv_text_21, R.id.tv_text_22, R.id.tv_text_23, R.id.tv_text_24, R.id.tv_text_25
            , R.id.tv_text_26, R.id.tv_text_27, R.id.tv_text_28, R.id.tv_text_29, R.id.tv_text_30
    })
    public void clickView(View view) {
        int index = 0;
        for (TextView textView : mTextViews) {
            if (textView == view) {
                boolean isSelect = mData.get(index).isSelect();
                textView.setSelected(!isSelect);
                mData.get(index).setSelect(!isSelect);
                break;
            }
            index++;
        }
    }


    @Override
    public void onRequestSuccess(List<Target> targets) {
        mData = targets;

        Random random = new Random();
        Resources resources = mContext.getResources();
        if (targets != null && targets.size() > 0) {

            int length = targets.size();
            for (int i = 0; i < length; i++) {
                TextView textView = mTextViews.get(i);
                textView.setVisibility(View.VISIBLE);
                int color = random.nextInt(2);
                textView.setBackgroundResource(color == 1 ? R.drawable.bg_complete_target_2 : R.drawable.bg_complete_target_1);
                textView.setTextColor(resources.getColorStateList(color == 1 ? R.color.complete_target_text_2 : R.color.complete_target_text_1));
                if(getActivity().getIntent()!=null&&(getActivity().getIntent().getIntExtra("mType",1)==4)){
                    textView.setText(targets.get(i).getTag());
                }else {
                    textView.setText(targets.get(i).getTitle());
                }
            }
//            for (int i=targets.size()-1;i<mTextViews.size();i++){
//                mTextViews.get(i).setVisibility(View.GONE);
//            }
        }
    }

    @Override
    public void onRequestNoData(String message) {

    }

    @Override
    public void onRequestError(String error_message) {

    }

    @Override
    public void onCaseSuccess(List<Target> list) {

    }

    @Override
    public void onCaseFailed(String msg) {

    }
}
